YUI.add("participantsList", function(Y) {
    var Lang = Y.Lang,
        Node = Y.Node, 
        log = Y.log,
        ParticipantEvent = Y.ParticipantEvent;
 
    /* ParticipantsList constructor */
    var ParticipantsList = function(config) {
        ParticipantsList.superclass.constructor.apply(this, arguments);
    } 
    ParticipantsList.NAME = "participantsList";
    ParticipantsList.ITEM_CLASS_NAME = ".participantsListItem";
   
 
    /* Default ParticipantsList Attributes */
    ParticipantsList.ATTRS = { 
    	 list : {
            value: null
        }
    };
 
    /* Extend the base Widget class */
    Y.extend(ParticipantsList, Y.Widget, {
 		
        /*
         * Initialization Code: Sets up privately used state
         * properties, and publishes the events ParticipantsList introduces
         */
        initializer : function(config) { 
        	
        	this._view = new Y.ParticipantsListView({
        		container:this.get("contentBox")
        	});
        	this._eventHandles = {
        		listloaded : null,
        		removeParticipant : null,
                trigger: {
                    mouseMove : null 
                },
                listRendered:null
         	};
         	  
 			this._loadData();
        },
         
         /*_initParticipantBadge : function(){
         	Y.on(Y.ParticipantsListView.LIST_RENDERED  , function(){
				var pb = new Y.ParticipantBadge({
		     		triggerNodes:Y.ParticipantsList.ITEM_CLASS_NAME,
		     		shim:false,
		     		zIndex:2
		 		});
				pb.render();  
			});
         },*/
         
 		 _clearHandles : function() {
            var eventHandles = this._eventHandles;
 
            if (eventHandles.trigger.mouseMove) {
                eventHandles.trigger.mouseMove.detach();
                eventHandles.trigger.mouseMove = null;
            }
            if(eventHandles.removeParticipant)
            {
            	eventHandles.removeParticipant.detach();
            	eventHandles.removeParticipant = null
            }
 		 },
        /*
         * Destruction Code: Clears event handles, timers,
         * and current trigger information
         */
        destructor : function() {
        	_clearHandles();            
        },
		
        /*
         * bindUI is used to bind attribute change and dom event
         * listeners
         */
        bindUI : function() { 
        	var that = this; 
    	    this._eventHandles.listRendered = Y.on(ParticipantEvent.ListView.LIST_RENDERED , Y.bind(this._onListRendered, this));
        	this._eventHandles.trigger.listloaded = Y.on(ParticipantEvent.ListModel.ListLoaded, Y.bind(this._onListLoaded, this));
    		this._eventHandles.removeParticipant = Y.on(ParticipantEvent.REMOVE_PARTICIPANT ,Y.bind( this._onRemoveParticipant, this) );
			this._eventHandles.trigger.mouseMove = Y.on(ParticipantEvent.Badge.ROLLED_OVER,  Y.bind( this._onParticipantRollOver, this) );
        		  
        },
        
        /* GET THE data of the rolled over content then fires an event that
         * contains that data. The badge should catch this event to render it self 
         * with the right data.
         */
        _onParticipantRollOver : function(nodeId)
        {
        	var list = this.get('list');
        	var len = list.length;
        	/*****************************TO DOOOOOOOOOO read details*******************
			* the for loop should be done in a better way performance wise, 
			* instead of always starting from 0, it should start from 
			* the previously rolled over node cause the currently rolled over 
			* node should be close enough to the previously rolled over node
			*/
        	for(var i=0; i < len; i++)
        	{
        		var participantModel = list[i]; 
    			if(participantModel.get('GUID') == nodeId)
    			{
    				Y.fire(ParticipantEvent.List.POPUP_CONTENT_READY, participantModel );
    			}
        	}
        },
        _onListLoaded : function(list, src) {
        		this._eventHandles.trigger.listloaded.detach();
            	this._eventHandles.trigger.listloaded = null;
            	src = null;
            	 
            	this.set("list" , list);
        		this._onDataLoaded(list); 
        		
    	},
    	/*
    	 * used by plugin to execute a function right after this function gets called
    	 */
    	_onListRendered : function()
    	{ 
    		this._eventHandles.listRendered.detach();
            this._eventHandles.listRendered = null;
    	},
 		_onRemoveParticipant : function (guid)
 		{
 			var list = this.get("list"); 
 			for(var i=0; i < list.length; i++)
 			{
 				var participantModel = list[i];  
    			if((LOCAL && participantModel.get('GUID').indexOf(guid) == 0)|| participantModel.get('GUID') == guid)
    			{ 
 					list.splice(i,1);
 					if(LOCAL)return;
 				}
 			}
 		},
        /*
         * syncUI is used to update the rendered DOM, based on the current
         * ParticipantsList state
         */
        syncUI : function() { 
        }, 
        //loads the list of participants
        _loadData : function() {
        	var particiapantsListModel = new Y.ParticipantsListModel();
        },
  		//what happens when the data is loaded
  		_onDataLoaded : function(list) {
        	 this._view.render(list);			
  		},
  		
  		_clearHandles : function() {
            var eventHandles = this._eventHandles;
            
            if(eventHandles.listRendered)
            {
            	eventHandles.listRendered.detach();
            	eventHandles.listRendered = null;
            }
            if(eventHandles.trigger.listloaded)
            {
            	eventHandles.trigger.listloaded.detach();
            	eventHandles.trigger.listloaded = null;
            }
            
            if(eventHandles.trigger.mouseMove)
            {
            	eventHandles.trigger.mouseMove.detach();
            	eventHandles.trigger.mouseMove = null;
            }
            if(eventHandles.removeParticipant)
            {
            	eventHandles.removeParticipant.detach();
            	eventHandles.removeParticipant = null;
            }
  		}
    }); 
 
	Y.ParticipantsList = ParticipantsList; 
//});
} , 0.1  );